<?php
$error;
if($_GET){
$groupname=trim($_GET['groupname']);//NPSN
$title=trim($_GET['title']);//NAMA SEKOLAh
$description=$title;
$is_public=TRUE;
	if(!$groupname){
		echo "parameter groupname tidak ada";
	}if(!$title){
		echo " parameter title tidak ada";
	}
	
	$ip=$_SERVER['REMOTE_ADDR'];

	echo "<br>";
	echo $ip;
	echo "<br>";
	echo $groupname;
	echo "<br>";
	echo $title;
	$error=FALSE;
	
		
		if( mb_strlen($title)<3 || mb_strlen($title)>30 ) {
			return;
			echo 'panjang title tidak sesuai';
		}
		elseif( preg_match('/[^a-zа-я0-9\-\.\s]/iu', $title) ) {
			$error=TRUE;
			echo 'title mengandung karakter aneh';
		}
		else {
			$db2->query('SELECT id FROM groups WHERE (groupname="'.$title.'" OR title="'.$title.'") LIMIT 1');
			if( $db2->num_rows() > 0 ) {
				$error=TRUE;
				echo "title sudah ada pada DB";
			}
		}
		
		if( ! $error ) {
			if( mb_strlen($groupname)<3 || mb_strlen($groupname)>30 ) {
				$error	= TRUE;
				echo 'panjang nama group tidak sesuai';
			}
			elseif( ! preg_match('/^[a-z0-9\-\_]{3,30}$/iu', $groupname) ) {
				$error	= TRUE;
				echo 'nama group mengandung karakter aneh';
			}
			else {
				$db2->query('SELECT id FROM groups WHERE (groupname="'.$groupname.'" OR title="'.$groupname.'") LIMIT 1');
				if( $db2->num_rows() > 0 ) {
					$error	= TRUE;
					echo 'nama group sudah ada di DB';
				}
				else {
				}
			}
		}
		
		if( ! $error ) {
			$db2->query('INSERT INTO groups SET groupname="'.$groupname.'", title="'.$title.'", about_me="'.$description.'", is_public="1" ');
			
			//$g	= $this->network->get_group_by_id( intval($db2->insert_id()) );
			$group_id=$this->db2->fetch_field('SELECT id FROM groups WHERE (groupname="'.$groupname.'")');
			
			$this->db2->query('INSERT INTO groups_admins SET group_id="'.$group_id.'", user_id="1" ');			
			
			$this->db2->query('INSERT INTO groups_followed SET user_id="1", group_id="'.$group_id.'", date="'.time().'"');;
			$this->db2->query('UPDATE groups SET num_followers="1" WHERE (groupname="'.$groupname.'")');
				echo "group create berhasil";	
		}
	
}
else{
	if(!$_GET){
	echo "GET tidak terdeteksi";
	}
}
?>
